package album

import (
	"babyplan/internal/common/constant"
	"babyplan/model"
	"cftgo/cftc"
	"cftgo/cftf/boot/app"
	"cftgo/cftf/db"
	"cftgo/cftf/ginhelper"
	"cftgo/cftf/uuid"
	"errors"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type Album struct {
}

func (this *Album) Search(c *gin.Context) {
	rspObj := &struct {
		albums []model.Album
	}{}
	result := db.GetOrmDb(constant.BABYPLANT_DB).Find(&rspObj.albums)
	if result.Error != nil {
		if errors.Is(result.Error, gorm.ErrRecordNotFound) {
		} else {
			ginhelper.RspErr(c, 500, "server internal error")
			return
		}
	}
	ginhelper.RspData(c, rspObj)
}

func (this *Album) Create(c *gin.Context) {
	reqObj := struct {
		Name string `json:"name"`
		Path string `json:"path"`
	}{}
	if err := c.ShouldBindJSON(&reqObj); err != nil {
		ginhelper.RspErr(c, 1, "params format error")
		return
	}
	if reqObj.Name == "" {
		ginhelper.RspErr(c, 1, "params format error")
		return
	}
	now := app.GetRealSeconds()
	album := model.Album{
		EntityId:  cftc.EntityId(uuid.NewLockNodeUuid()),
		Name:      reqObj.Name,
		Path:      reqObj.Path,
		CreatedAt: now,
		UpdatedAt: now,
	}
	result := db.GetOrmDb(constant.BABYPLANT_DB).Create(&album)
	if result.Error != nil {
		ginhelper.RspErr(c, 500, "server internal error")
		return
	}
	ginhelper.RspOk(c)
}
